/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pfc.arbol;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import org.apache.commons.lang.StringUtils;
import pfc.comun.IDEpfc;

/**
 *
 * @author xandru
 */
public class FileTreeNode extends DefaultMutableTreeNode {

    File _file;
    JPopupMenu popupMenu;
    boolean _isSourceParent = false;
    boolean menuContexto = false;

    public FileTreeNode(File file) {
        super(file.getName());
        this._file = file;
        //construirMenuContexto();
    }

    public void click(java.awt.event.MouseEvent e) {

        if (!menuContexto) {
            construirMenuContexto();
            menuContexto = true;
        }

        if (e.getButton() == MouseEvent.BUTTON3) {

            mostrarMenuContexto(e);

        }

    }

    ;

    /*
    private void mostrarMenuContexto(MouseEvent e) {
    if (popupMenu!=null)
    popupMenu.show( IDEpfc.app , e.getXOnScreen(), e.getYOnScreen());
    }
     */
    protected void construirMenuContexto() {
        popupMenu = null;
        final FileTreeNode ftn = this;

        //  System.out.println("FileTreeNode " + this.toString() +" " + this._isSourceParent);
        JMenuItem mi = null;
        if (this._file.isDirectory()) {
            if (this._isSourceParent) {
                mi = new JMenuItem("Nuevo Fichero");
                mi.setActionCommand("nuevoFichero");
                mi.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        nuevoFichero(e, ftn);
                    }
                });
            }
        } else {
            mi = new JMenuItem("Editar");
            mi.setActionCommand("editarFichero");
            mi.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        editarFichero(e, ftn);
                    }
                });

        }

        if (mi != null) {
            popupMenu = new JPopupMenu();
            popupMenu.add(mi);
            mi.setEnabled(true);

        }
    }

    private void mostrarMenuContexto(MouseEvent e) {
        if (popupMenu != null) {
            popupMenu.show(IDEpfc.app, e.getXOnScreen(), e.getYOnScreen());
        }
    }

    public File getFile() {
        return _file;
    }

    public boolean isSourceParent() {
        return this._isSourceParent;
    }

    public void setSourceParent(boolean b) {
        this._isSourceParent = b;
    }

    private void editarFichero(ActionEvent e, FileTreeNode aThis) {
        System.out.println("Propiedades de " + this.getClass().getName());
        pfc.gui.FileEdit fEdit = new pfc.gui.FileEdit(this);

        IDEpfc.app.addParaEditar(fEdit,  StringUtils.capitalize(aThis.getFile().getName()));
    }

    private void nuevoFichero(ActionEvent e, FileTreeNode aThis) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    void directoryCreated(File file) {
        System.out.println("Directorio creado debajo de " + this._file + " creado-> " + file.getAbsolutePath());
        FileTreeNode ftn = new FileTreeNode(file);
        this.add(ftn);

    }

    void fileCreated(File file) {
        System.out.println("Fichero creado " + file.getAbsolutePath());
    }

    void directoryDeleted(File file) {
        System.out.println("Directorio eliminado " + file.getAbsolutePath());
    }

    void fileDeleted(File file) {
        System.out.println("Fichero eliminado " + file.getAbsolutePath());
    }
}
